package core
{

	import beans.WaveBean;

	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import globals.Global;

	public class BattleManager
	{
		private var waveTimer:Timer;
		private var waveList:Array;
		private var monsterTotal:uint;

		private var life:uint = 3;
		private var ids:uint = 1;

		public function BattleManager()
		{
			waveTimer = new Timer(1000);
			waveTimer.addEventListener(TimerEvent.TIMER, timerHandler);
			waveTimer.addEventListener(TimerEvent.TIMER_COMPLETE, finishHandler);

			waveList = new Array();
			var wave:WaveBean

			for(var i:int = 0; i < 25; i++)
			{
				wave = new WaveBean();
				wave.id = i;
				wave.monsterCount = 10 + i * 2;
//				wave.monsterCount = 1;
				wave.monsterFrequency = 1000;
				waveList.push(wave);
			}
		}

		public function startWave():void
		{
			var waveBean:WaveBean = waveList.shift() as WaveBean;
			monsterTotal = waveBean.monsterCount;
			waveTimer.reset();
			waveTimer.delay = waveBean.monsterFrequency;
			waveTimer.repeatCount = waveBean.monsterCount;
			waveTimer.start();

			Global.uiManager.scoreFace.updateWave(25);
			Global.uiManager.bottomFace.updateMonster(monsterTotal);
		}

		public function endWave():void
		{
			if(waveList.length > 0)
			{
				startWave();
			}
			else
			{
				endBattle();
			}
		}

		private function endBattle():void
		{
			trace("end battle");
		}

		public function monsterDie(mid:String):void
		{
			monsterTotal--;

			if(monsterTotal <= 0)
			{
				endWave();
			}
			else
			{
				Global.uiManager.bottomFace.updateMonster(monsterTotal);
			}
		}

		public function monsterPass(mid:String):void
		{
			monsterTotal--;

			if(--life <= 0)
			{
//				endBattle();
			}
		}

		public function timerHandler(event:TimerEvent):void
		{
			Global.unitManager.addMonsterTest(ids);
			ids++;
		}

		public function finishHandler(event:TimerEvent):void
		{

		}
	}
}